﻿using Core.ECS.Components;
using Core.Log;
using Core.Rendering.Buffer;
using Core.Rendering.Resources;
using OpenTK.Windowing.Desktop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Core.ECS
{
    [DataContract]
    public class Scene
    {
        public static Scene Instance
        {
            get
            {
                if (_instance != null) return _instance;
                else
                {
                    _instance = new Scene();
                    return _instance;
                }
            }
        }
        private static Scene? _instance = null;
        public static GameWindow? Window;
        [DataMember]
        public CCamera? MainCamera;
        [DataMember]
        public List<Actor> ActorsList;

        public UniformBufferObject? UniformBufferObject;
        public Dictionary<Guid, Actor> Actors;
        public Dictionary<string, Actor> ActorsByName;
        public static Dictionary<string, Material> Materials = new();
        public static Dictionary<string, Model> Models = new();
        public Scene()
        {
            Actors = new();
            ActorsList = new();
        }
        [OnSerializing]
        public void OnSerializing(StreamingContext _)
        {
        }
        [OnDeserialized]
        public void OnDeserialized(StreamingContext _)
        {
            Scene._instance = this;
            Materials = new();
            Models = new();
            Actors = new();
            ActorsByName = new();
            foreach (var actor in ActorsList)
            {
                if (!Actors.ContainsKey(actor.Id))
                {
                    Actors.Add(actor.Id, actor);
                }
                if (!ActorsByName.ContainsKey(actor.Name))
                {
                    ActorsByName.Add(actor.Name, actor);
                }
                else
                {
                    ExLog.InfoLogCore("Actor have Same Id " + actor.Id);
                }
            }
        }
    }
}
